Skip to main content

processReminder

processReminder

  1. Start Reminder Process

    • Capture current time as now using moment().
  2. Fetch All Open Rating Notifications

    • Query the ratingNotificationRepo for all records:

      • Where status = OPEN

      • Selects key fields:

        • id, type, cycleType, status, appointmentId, customerId, sentAt, isInternal
    • Result: allOpenNotifications[]

  3. Extract Unique Customer IDs

    • From allOpenNotifications, extract and deduplicate:

      • customerId → ensure valid numbers
    • Result: allCustomerIds[]

    • Condition:

      • If allCustomerIds is empty:

        • Return early with { success: true, sent: 0, closed: 0, summary: [] } using logAndReturn()
  4. Fetch Customer Notification Preferences

    • Query notificationPreferenceRepo where:

      • customer.id in allCustomerIds
      • notificationService.name = 'rating'
      • notificationType in [EMAIL, SMS]
      • status = OPTED_OUT
    • Includes relations: customer, notificationService

    • Result: customerPreferences[]

  5. Build Opt-Out Map

    • Create Map<number, Set<NotificationType>>:

      • For each preference in customerPreferences:

        • Add notificationType to the corresponding customerId in the map
    • Result: optedOutMap

  6. Filter Notifications

    • Call filterNotifications(allOpenNotifications, optedOutMap, now):

      • Purpose:

        • Filter out:

          • Notifications due for sending
          • Notifications to cancel
        • Based on:

          • Opt-out settings (optedOutMap)
          • Time elapsed since last sent (now)
          • Notification cycle phase (cycleType)
      • Result:

        • notificationsToSend[]: those eligible to be sent
        • notificationsToClose[]: those to be closed/cancelled
  7. Fetch Related Appointments

    • Call fetchAppointmentsForNotifications(notificationsToSend):

      • Retrieves appointments (internal or external) linked to each notification
      • Returns a Map<appointmentId, Appointment>
    • Result: appointmentMap

  8. Send Notifications & Build Summary

    • Call sendNotificationsAndBuildSummary(notificationsToSend, appointmentMap):

      • Sends actual notifications (email/SMS)

      • Builds records for update:

        • notificationsToUpdate[]: with new sentAt, updated cycleType
        • summaryMap: keyed summary of sent notifications
    • Result:

      • notificationsToUpdate[]
      • summaryMap
  9. Update Processed Notification Records

    • If notificationsToUpdate.length > 0:

      • Increment Prometheus metric ratingsTypeProcessedCounter.labels('reminder') by count
    • Batch Update Notification Statuses

      • For each item in notificationsToUpdate[]:

        • Update sentAt and cycleType using ratingNotificationRepo.update()
  10. Close Expired Notifications

    • If notificationsToClose.length > 0:

      • Update status = CANCELLED where id in notificationsToClose
  11. Return Process Summary

    • Use logAndReturn() to return and log the result:

      {
      success: true,
      sent: <number of notifications sent>,
      closed: <number of notifications closed>,
      summary: <array of summaryMap values>,
      }